﻿using System.Text;
using System.Threading.Tasks;

namespace ThingsGateway.Foundation
{
    /// <summary>
    /// TCP读写设备
    /// </summary>
    public abstract class ReadWriteDevicesTcpClientBase : TcpClient, IReadWriteDevice
    {
        #region Public Properties

        public IByteConverter ByteConverter
        {
            get { return (IByteConverter)GetValue(ReadWriteDevicesEx.ByteConverterProperty); }
        }

        public ushort TimeOut
        {
            get { return (ushort)GetValue(ReadWriteDevicesEx.TimeOutProperty); }
        }
        public ushort ConnectTimeOut
        {
            get { return (ushort)GetValue(ReadWriteDevicesEx.ConnectTimeOutProperty); }
        }
        public ushort WordLength
        {
            get { return (ushort)GetValue(ReadWriteDevicesEx.WordLengthProperty); }
        }

        #endregion Public Properties

        #region Public Methods

        public ushort GetWordLength(string address, int length, int dataTypeLength)
        {
            if (WordLength != 0)
            {
                return (ushort)(WordLength * length * dataTypeLength);
            }

            int num = length * dataTypeLength * 2 / 4;
            return num == 0 ? (ushort)1 : (ushort)num;
        }

        public abstract Result<byte[]> Read(string address, ushort length);

        public async Task<Result<byte[]>> ReadAsync(string address, ushort length)
        {
            Result<byte[]> operateResult = await Task.FromResult(Read(address, length));
            //Result<byte[]> operateResult = await Task.Run(() => Read(address, length));
            return operateResult;
        }

        public abstract Result<bool[]> ReadBool(string address, ushort length);

        public virtual Result<bool> ReadBool(string address)
        {
            return ByteConverterHelper.GetResultFromArray<bool>(ReadBool(address, 1));
        }

        public async Task<Result<bool[]>> ReadBoolAsync(string address, ushort length)
        {
            Result<bool[]> operateResult = await Task.FromResult(ReadBool(address, length));
            return operateResult;
        }

        public virtual async Task<Result<bool>> ReadBoolAsync(string address)
        {
            return await Task.FromResult(ReadBool(address));
        }

        public Result<double> ReadDouble(string address)
        {
            return ByteConverterHelper.GetResultFromArray<double>(ReadDouble(address, 1));
        }

        public Result<double[]> ReadDouble(string address, ushort length)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<double[]>(Read(address, GetWordLength(address, length, 4)), m => transformParameter.ToDouble(m, 0, length));
        }

        public async Task<Result<double>> ReadDoubleAsync(string address)
        {
            Result<double[]> result = await ReadDoubleAsync(address, 1);
            return ByteConverterHelper.GetResultFromArray<double>(result);
        }

        public async Task<Result<double[]>> ReadDoubleAsync(string address, ushort length)
        {
            Result<byte[]> result = await ReadAsync(address, GetWordLength(address, length, 4));
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<double[]>(result, m => transformParameter.ToDouble(m, 0, length));
        }

        public Result<float> ReadFloat(string address)
        {
            return ByteConverterHelper.GetResultFromArray<float>(ReadFloat(address, 1));
        }

        public Result<float[]> ReadFloat(string address, ushort length)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<float[]>(Read(address, GetWordLength(address, length, 2)), m => transformParameter.ToSingle(m, 0, length));
        }

        public async Task<Result<float>> ReadFloatAsync(string address)
        {
            Result<float[]> result = await ReadFloatAsync(address, 1);
            return ByteConverterHelper.GetResultFromArray<float>(result);
        }

        public async Task<Result<float[]>> ReadFloatAsync(string address, ushort length)
        {
            Result<byte[]> result = await ReadAsync(address, GetWordLength(address, length, 2));
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<float[]>(result, m => transformParameter.ToSingle(m, 0, length));

        }

        public Result<short> ReadInt16(string address)
        {
            return ByteConverterHelper.GetResultFromArray<short>(ReadInt16(address, 1));
        }

        public Result<short[]> ReadInt16(string address, ushort length)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<short[]>(Read(address, GetWordLength(address, length, 1)), m => transformParameter.ToInt16(m, 0, length));
        }

        public async Task<Result<short>> ReadInt16Async(string address)
        {
            Result<short[]> result = await ReadInt16Async(address, 1);
            return ByteConverterHelper.GetResultFromArray<short>(result);
        }

        public async Task<Result<short[]>> ReadInt16Async(string address, ushort length)
        {
            Result<byte[]> result = await ReadAsync(address, GetWordLength(address, length, 1));
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<short[]>(result, m => transformParameter.ToInt16(m, 0, length));
        }

        public Result<int> ReadInt32(string address)
        {
            return ByteConverterHelper.GetResultFromArray<int>(ReadInt32(address, 1));
        }

        public Result<int[]> ReadInt32(string address, ushort length)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<int[]>(Read(address, GetWordLength(address, length, 2)), m => transformParameter.ToInt32(m, 0, length));
        }

        public async Task<Result<int>> ReadInt32Async(string address)
        {
            Result<int[]> result = await ReadInt32Async(address, 1);
            return ByteConverterHelper.GetResultFromArray<int>(result);
        }

        public async Task<Result<int[]>> ReadInt32Async(string address, ushort length)
        {
            Result<byte[]> result = await ReadAsync(address, GetWordLength(address, length, 2));
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<int[]>(result, m => transformParameter.ToInt32(m, 0, length));

        }

        public Result<long> ReadInt64(string address)
        {
            return ByteConverterHelper.GetResultFromArray<long>(ReadInt64(address, 1));
        }

        public Result<long[]> ReadInt64(string address, ushort length)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<long[]>(Read(address, GetWordLength(address, length, 4)), m => transformParameter.ToInt64(m, 0, length));

        }

        public async Task<Result<long>> ReadInt64Async(string address)
        {
            Result<long[]> result = await ReadInt64Async(address, 1);
            return ByteConverterHelper.GetResultFromArray<long>(result);
        }

        public async Task<Result<long[]>> ReadInt64Async(string address, ushort length)
        {
            Result<byte[]> result = await ReadAsync(address, GetWordLength(address, length, 4));
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<long[]>(result, m => transformParameter.ToInt64(m, 0, length));

        }

        public Result<string> ReadString(string address, ushort length)
        {
            return ReadString(address, length, Encoding.ASCII);
        }

        public virtual Result<string> ReadString(string address, ushort length, Encoding encoding)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(
                ref address, ByteConverter, out Encoding encoding1, out ushort length1, out BCDFormat bCDFormat1
                );
            length = length1 == 0 ? length : length1;
            encoding = encoding1 == null ? encoding : encoding1;
            return ByteConverterHelper.GetResultFromBytes<string>(Read(address, length), m => transformParameter.ToString(m, 0, m.Length, encoding));

        }

        public async Task<Result<string>> ReadStringAsync(string address, ushort length)
        {
            Result<string> operateResult = await ReadStringAsync(address, length, Encoding.ASCII);
            return operateResult;
        }

        public async Task<Result<string>> ReadStringAsync(string address, ushort length, Encoding encoding)
        {
            return await Task.FromResult(ReadString(address, length, encoding));

        }

        public Result<ushort> ReadUInt16(string address)
        {
            return ByteConverterHelper.GetResultFromArray<ushort>(ReadUInt16(address, 1));

        }

        public Result<ushort[]> ReadUInt16(string address, ushort length)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<ushort[]>(Read(address, GetWordLength(address, length, 1)), m => transformParameter.ToUInt16(m, 0, length));

        }

        public async Task<Result<ushort>> ReadUInt16Async(string address)
        {
            Result<ushort[]> result = await ReadUInt16Async(address, 1);
            return ByteConverterHelper.GetResultFromArray<ushort>(result);
        }

        public async Task<Result<ushort[]>> ReadUInt16Async(string address, ushort length)
        {
            Result<byte[]> result = await ReadAsync(address, GetWordLength(address, length, 1));
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<ushort[]>(result, m => transformParameter.ToUInt16(m, 0, length));
        }

        public Result<uint> ReadUInt32(string address)
        {
            return ByteConverterHelper.GetResultFromArray<uint>(ReadUInt32(address, 1));

        }

        public Result<uint[]> ReadUInt32(string address, ushort length)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<uint[]>(Read(address, GetWordLength(address, length, 2)), m => transformParameter.ToUInt32(m, 0, length));

        }

        public async Task<Result<uint>> ReadUInt32Async(string address)
        {
            Result<uint[]> result = await ReadUInt32Async(address, 1);
            return ByteConverterHelper.GetResultFromArray<uint>(result);
        }

        public async Task<Result<uint[]>> ReadUInt32Async(string address, ushort length)
        {
            Result<byte[]> result = await ReadAsync(address, GetWordLength(address, length, 2));
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<uint[]>(result, m => transformParameter.ToUInt32(m, 0, length));

        }

        public Result<ulong> ReadUInt64(string address)
        {
            return ByteConverterHelper.GetResultFromArray<ulong>(ReadUInt64(address, 1));

        }

        public Result<ulong[]> ReadUInt64(string address, ushort length)
        {

            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<ulong[]>(Read(address, GetWordLength(address, length, 4)), m => transformParameter.ToUInt64(m, 0, length));

        }

        public async Task<Result<ulong>> ReadUInt64Async(string address)
        {
            Result<ulong[]> result = await ReadUInt64Async(address, 1);
            return ByteConverterHelper.GetResultFromArray<ulong>(result);
        }

        public async Task<Result<ulong[]>> ReadUInt64Async(string address, ushort length)
        {
            Result<byte[]> result = await ReadAsync(address, GetWordLength(address, length, 4));
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return ByteConverterHelper.GetResultFromBytes<ulong[]>(result, m => transformParameter.ToUInt64(m, 0, length));

        }

        public Result<TimeSpan> Wait(string address, bool waitValue, int readInterval, int waitTimeout)
        {
            return ReadWriteHelper.Wait(this, address, waitValue, readInterval, waitTimeout);

        }

        public Result<TimeSpan> Wait(string address, short waitValue, int readInterval, int waitTimeout)
        {
            return ReadWriteHelper.Wait(this, address, waitValue, readInterval, waitTimeout);

        }

        public Result<TimeSpan> Wait(string address, ushort waitValue, int readInterval, int waitTimeout)
        {
            return ReadWriteHelper.Wait(this, address, waitValue, readInterval, waitTimeout);

        }

        public Result<TimeSpan> Wait(string address, int waitValue, int readInterval, int waitTimeout)
        {
            return ReadWriteHelper.Wait(this, address, waitValue, readInterval, waitTimeout);

        }

        public Result<TimeSpan> Wait(string address, uint waitValue, int readInterval, int waitTimeout)
        {
            return ReadWriteHelper.Wait(this, address, waitValue, readInterval, waitTimeout);

        }

        public Result<TimeSpan> Wait(string address, long waitValue, int readInterval, int waitTimeout)
        {
            return ReadWriteHelper.Wait(this, address, waitValue, readInterval, waitTimeout);

        }

        public Result<TimeSpan> Wait(string address, ulong waitValue, int readInterval, int waitTimeout)
        {
            return ReadWriteHelper.Wait(this, address, waitValue, readInterval, waitTimeout);

        }

        public async Task<Result<TimeSpan>> WaitAsync(string address, bool waitValue, int readInterval, int waitTimeout)
        {
            Result<TimeSpan> operateResult = await ReadWriteHelper.WaitAsync(this, address, waitValue, readInterval, waitTimeout);
            return operateResult;
        }

        public async Task<Result<TimeSpan>> WaitAsync(string address, short waitValue, int readInterval, int waitTimeout)
        {
            Result<TimeSpan> operateResult = await ReadWriteHelper.WaitAsync(this, address, waitValue, readInterval, waitTimeout);
            return operateResult;
        }

        public async Task<Result<TimeSpan>> WaitAsync(string address, ushort waitValue, int readInterval, int waitTimeout)
        {
            Result<TimeSpan> operateResult = await ReadWriteHelper.WaitAsync(this, address, waitValue, readInterval, waitTimeout);
            return operateResult;
        }

        public async Task<Result<TimeSpan>> WaitAsync(string address, int waitValue, int readInterval, int waitTimeout)
        {
            Result<TimeSpan> operateResult = await ReadWriteHelper.WaitAsync(this, address, waitValue, readInterval, waitTimeout);
            return operateResult;
        }

        public async Task<Result<TimeSpan>> WaitAsync(string address, uint waitValue, int readInterval, int waitTimeout)
        {
            Result<TimeSpan> operateResult = await ReadWriteHelper.WaitAsync(this, address, waitValue, readInterval, waitTimeout);
            return operateResult;
        }

        public async Task<Result<TimeSpan>> WaitAsync(string address, long waitValue, int readInterval, int waitTimeout)
        {
            Result<TimeSpan> operateResult = await ReadWriteHelper.WaitAsync(this, address, waitValue, readInterval, waitTimeout);
            return operateResult;
        }

        public async Task<Result<TimeSpan>> WaitAsync(string address, ulong waitValue, int readInterval, int waitTimeout)
        {
            Result<TimeSpan> operateResult = await ReadWriteHelper.WaitAsync(this, address, waitValue, readInterval, waitTimeout);
            return operateResult;
        }

        public abstract Result Write(string address, byte[] value);

        public abstract Result Write(string address, bool[] value);

        public Result Write(string address, bool value)
        {
            return Write(address, new bool[1]
{
      value
});
        }

        public Result Write(string address, short value)
        {
            return Write(address, new short[1]
{
      value
});
        }
        public Result Write(string address, byte value)
        {
            return Write(address, new byte[1]
{
      value
});
        }
        public Result Write(string address, short[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return Write(address, transformParameter.GetBytes(values));
        }

        public Result Write(string address, ushort value)
        {
            return Write(address, new ushort[1]
{
      value
});
        }

        public Result Write(string address, ushort[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return Write(address, transformParameter.GetBytes(values));
        }

        public Result Write(string address, int value)
        {
            return Write(address, new int[1]
{
      value
});
        }

        public Result Write(string address, int[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return Write(address, transformParameter.GetBytes(values));

        }

        public Result Write(string address, uint value)
        {
            return Write(address, new uint[1]
{
      value
});
        }

        public Result Write(string address, uint[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return Write(address, transformParameter.GetBytes(values));
        }

        public Result Write(string address, long value)
        {
            return Write(address, new long[1]
{
      value
});
        }

        public Result Write(string address, long[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return Write(address, transformParameter.GetBytes(values));

        }

        public Result Write(string address, ulong value)
        {
            return Write(address, new ulong[1]
{
      value
});
        }

        public Result Write(string address, ulong[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return Write(address, transformParameter.GetBytes(values));

        }

        public Result Write(string address, float value)
        {
            return Write(address, new float[1]
{
      value
});
        }

        public Result Write(string address, float[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return Write(address, transformParameter.GetBytes(values));

        }

        public Result Write(string address, double value)
        {
            return Write(address, new double[1]
{
      value
});
        }

        public Result Write(string address, double[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            return Write(address, transformParameter.GetBytes(values));

        }

        public Result Write(string address, string value)
        {
            return Write(address, value, Encoding.UTF8);

        }

        public virtual Result Write(string address, string value, Encoding encoding)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            byte[] data = transformParameter.GetBytes(value, encoding);
            if (WordLength == 1)
            {
                data = GenericHelper.ArrayExpandToLengthEven<byte>(data);
            }

            return Write(address, data);
        }

        public Result Write(string address, string value, int length)
        {
            return Write(address, value, length, Encoding.ASCII);

        }

        public Result Write(string address, string value, int length, Encoding encoding)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            byte[] data = transformParameter.GetBytes(value, encoding);
            if (WordLength == 1)
            {
                data = GenericHelper.ArrayExpandToLengthEven<byte>(data);
            }
            byte[] length1 = GenericHelper.ArrayExpandToLength<byte>(data, length);
            return Write(address, length1);
        }

        public async Task<Result> WriteAsync(string address, byte[] value)
        {
            Result operateResult = await Task.FromResult(Write(address, value));
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, bool[] value)
        {
            Result operateResult = await Task.FromResult(Write(address, value));
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, bool value)
        {
            Result operateResult = await WriteAsync(address, new bool[1]
            {
        value
            });
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, short value)
        {
            Result operateResult = await WriteAsync(address, new short[1]
        {
        value
        });
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, short[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            Result operateResult = await WriteAsync(address, transformParameter.GetBytes(values));
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, ushort value)
        {
            Result operateResult = await WriteAsync(address, new ushort[1]
        {
        value
        });
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, ushort[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            Result operateResult = await WriteAsync(address, transformParameter.GetBytes(values));
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, int value)
        {
            Result operateResult = await WriteAsync(address, new int[1]
        {
        value
        });
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, int[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            Result operateResult = await WriteAsync(address, transformParameter.GetBytes(values));
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, uint value)
        {
            Result operateResult = await WriteAsync(address, new uint[1]
        {
        value
        });
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, uint[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            Result operateResult = await WriteAsync(address, transformParameter.GetBytes(values));
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, long value)
        {
            Result operateResult = await WriteAsync(address, new long[1]
        {
        value
        });
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, long[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            Result operateResult = await WriteAsync(address, transformParameter.GetBytes(values));
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, ulong value)
        {
            Result operateResult = await WriteAsync(address, new ulong[1]
    {
        value
    });
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, ulong[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            Result operateResult = await WriteAsync(address, transformParameter.GetBytes(values));
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, float value)
        {
            Result operateResult = await WriteAsync(address, new float[1]
{
        value
});
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, float[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            Result operateResult = await WriteAsync(address, transformParameter.GetBytes(values));
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, double value)
        {
            Result operateResult = await WriteAsync(address, new double[1]
{
        value
});
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, double[] values)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            Result operateResult = await WriteAsync(address, transformParameter.GetBytes(values));
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, string value)
        {
            Result operateResult = await WriteAsync(address, value, Encoding.ASCII);
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, string value, Encoding encoding)
        {
            Result operateResult = await WriteAsync(address, value, encoding);
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, string value, int length)
        {
            Result operateResult = await WriteAsync(address, value, length, Encoding.ASCII);
            return operateResult;
        }

        public async Task<Result> WriteAsync(string address, string value, int length, Encoding encoding)
        {
            IByteConverter transformParameter = ByteConverterHelper.ExtractTransformParameter(ref address, ByteConverter);
            byte[] temp = transformParameter.GetBytes(value, encoding);
            if (WordLength == 1)
            {
                temp = GenericHelper.ArrayExpandToLengthEven<byte>(temp);
            }

            temp = GenericHelper.ArrayExpandToLength<byte>(temp, length);
            Result operateResult = await WriteAsync(address, temp);
            temp = null;
            return operateResult;
        }

        #endregion Public Methods

        #region Protected Methods

        protected override void OnConnected(MsgEventArgs e)
        {
            base.OnConnected(e);
            this.PluginsManager.UseReconnection(3000, (tcpclient, tryCount, ex) =>
            {
                LogNet?.Error(ToString(), ex);
                return true;
            }
        , (tcpClient) =>
        {
            LogNet?.Info(ToString(), "重新连接成功");
        });
        }

        #endregion Protected Methods
    }
}
